package com.bycx.wechat.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.bycx.dingxinlc.model.SmsValicode;
import com.bycx.dingxinlc.model.TCustLoginInfo;

/** Created by yiqr on 2017/6/7. */
public class WeChatSessionUtil {

	public final static String CURRENT_WECHAT_CODE = "WECHAT_USER_CODE";
	public final static String CURRENT_WECHAT_LOGIN = "WECHAT_USER_LOGIN";
	public final static String USER_SMSCODE = "USER_SMSCODE";// 短息验证码标识
	public final static String USER_OPENID = "USER_OPENID";

	// public final static String CURRENT_WECHAT_USER = "WECHAT_USER";
	// public final static String USER_SESSION = "LOGIN_USER";
	// public final static String USER_CUST_TYP = "USER_CUST_TYP";
	// //用户门店扫描信息
	// public final static String USER_SCAN_STORE = "USER_SCAN_STORE";
	//
	// public final static String MAP_LOCATION_LATITUDE = "MAP_LOCATION_LATITUDE";
	// public final static String MAP_LOCATION_LONGITUDE = "MAP_LOCATION_LONGITUDE";
	// public final static String MAP_LOCATION_PRECISION = "MAP_LOCATION_Precision";
	// public final static String MAP_LOCATION_TIME = "MAP_LOCATION_TIME";

	/** 获取当前Session */
	public static HttpSession getCurrentSession() {
		HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		if (req == null)
			return null;
		return req.getSession();
	}

	/** 获取session 授权code代码
	 * 
	 * @return */
	public static String getCode() {
		HttpSession session = getCurrentSession();
		if (session == null)
			return null;
		return (String) session.getAttribute(CURRENT_WECHAT_CODE);
	}

	/** 设置session 授权code代码
	 * 
	 * @param code */
	public static void setCode(String code) {
		HttpSession session = getCurrentSession();
		if (session != null)
			session.setAttribute(CURRENT_WECHAT_CODE, code);
	}

	/** 获取当前微信用户
	 * 
	 * @return */
	/*
	 * public static TCustBaseInfo getCurrentWeChatCust(){ try { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Object obj = request.getSession().getAttribute(CURRENT_WECHAT_USER); if(obj==null){ return null; } return (TCustBaseInfo)obj; } catch (Exception e) { // handle exception } return null; }
	 */

	/** 设置当前微信用户
	 * 
	 * @param custBaseInfo */
	/*
	 * public static void setCurrentWeChatCust(TCustBaseInfo custBaseInfo) {
	 * HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	 * request.getSession().setAttribute(CURRENT_WECHAT_USER, custBaseInfo);
	 * }
	 */

	/** 设置当前微信登录用户信息
	 * 
	 * @param loginInfo */
	public static void setCustLoginInfo(TCustLoginInfo loginInfo) {
		HttpSession session = getCurrentSession();
		if (session != null)
			session.setAttribute(CURRENT_WECHAT_LOGIN, loginInfo);
	}

	/** 获取当前微信登录用户信息
	 * 
	 * @return */
	public static TCustLoginInfo getCustLoginInfo() {
		HttpSession session = getCurrentSession();
		if (session == null)
			return null;
		return (TCustLoginInfo) session.getAttribute(CURRENT_WECHAT_LOGIN);
	}

	/** 检查微信用户是否登录 */
	public static boolean checkLogin() {
		TCustLoginInfo loginInfo = getCustLoginInfo();
		return loginInfo != null;
	}

	/** 设置短信验证码 */
	public static void setSmsValicode(SmsValicode code) {
		HttpSession session = getCurrentSession();
		if (session != null)
			session.setAttribute(USER_SMSCODE, code);
	}

	/** 获取短信验证码 */
	public static SmsValicode getSmsValicode() {
		HttpSession session = getCurrentSession();
		if (session == null)
			return null;
		return (SmsValicode) session.getAttribute(USER_SMSCODE);
	}

	/** 清除验证码 **/
	public static void delSmsValicode() {
		HttpSession session = getCurrentSession();
		if (session != null)
			session.removeAttribute(USER_SMSCODE);
	}

	// 设置openid
	public static void setOpenId(String openId) {
		HttpSession session = getCurrentSession();
		if (session != null)
			session.setAttribute(USER_OPENID, openId);
	}

	// 获取openid
	public static String getOpenId() {
		HttpSession session = getCurrentSession();
		if (session == null)
			return null;
		return (String) session.getAttribute(USER_OPENID);
	}
}
